<?php

namespace app\repositories\eloquent;
use app\repositories\contracts\AdminAuthInterface;
use app\admin\model\AdminRoot;
use app\facade\Password;
use think\facade\Session;
use think\response\Json;
use function foo\func;

/***
 * Class RootProvider
 * @package app\repositories\eloquent
 * @Author Vee7
 * @Date 2021/12/14
 * @Time 13:11
 * @Description 实现后台管理员接口
 */
class AdminAuthProvider implements AdminAuthInterface
{
    /***
     * @param array $loginData
     * @Author Vee7
     * @Date 2021/12/16
     * @Time 15:11
     * @Description 管理员登录验证
     */
    public function checkLogin($loginData)
    {
        $data = $loginData['loginData'];
        //检测验证码
        if(!captcha_check($data['verifyCode']))
        {
            // 验证失败
            return ['code'=>1,'msg'=>'验证码错误，点击可刷新验证码','data'=>''];
        }
        //密码验证
        $result = self::checkPassword($data['root_username'],$data['root_password']);
        if($result['bool'] !== true){
            return ['code'=>1,'msg'=>'密码错误，请检查后重试','data'=>$result];
        }
        //账户状态验证
        $isOpen = self::checkStatus($result['data']);
        if($isOpen['bool'] !== true){
            return ['code'=>1,'msg'=>'账户状态异常或被禁止','data'=>$isOpen];
        }
        Session::set('RootData', $isOpen['data']);
        return ['code'=>0,'msg'=>'登陆成功，即将跳转','data'=>$result['data']];
    }

    /***
     * @param $rootData
     * @Author Vee7
     * @Date 2021/12/22
     * @Time 11:02
     * @Description 检测账户状态是否正常
     */
    public function checkStatus($rootData)
    {
        if($rootData['status'] === 0)
        {
            $return['bool'] = false;
            $return['data'] = '';
            return $return;
        }else{
            $return['bool'] = true;
            $return['data'] = $rootData;
            return $return;
        }
    }

    /***
     * @Author Vee7
     * @Date 2021/12/16
     * @Time 15:28
     * @Description 检查管理员密码是否正确
     */
    public function checkPassword($username,$password)
    {
        //获取用户加密盐
        $rootData = self::getRootData($username);
        //对比用户密码
        $return['bool'] = Password::ComparePass($password,$rootData['root_password'],$rootData['salt']);
        $return['data'] = $rootData;
        return $return;
    }

    /***
     * @param $username
     * @Author Vee7
     * @Date 2021/12/16
     * @Time 15:32
     * @Description 获取用户信息
     */
    public function getRootData($username)
    {
        return AdminRoot::where('root_name',$username)->find()->toArray();
    }



}